package com.pillpoppers.pillbox.database;

import java.util.ArrayList;

import com.pillpoppers.pillbox.Contact;
import com.pillpoppers.pillbox.Schedule;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class pillDB 
{
	private static final String DATABASE_NAME = "pilltable.db";
	private static final int DATABASE_VERSION = 1;
	
	// database object and database helper object
    private SQLiteDatabase db;
    private PillDatabaseHelper dbHelper;
    
 // Constructor
    public pillDB(Context context)
    {
        dbHelper = new PillDatabaseHelper(context);
    }
    
    // Private methods
    private void openReadableDB()
    {
        db = dbHelper.getReadableDatabase();
    }
    
    private void openWriteableDB()
    {
        db = dbHelper.getWritableDatabase();
    }
    
    private void closeDB()
    {
        if (db != null)
            db.close();
    }
    
    /**
     * Gets all values from contact table.
     * @return ArrayList<Contact> with all values from contact database
     */
    public ArrayList<Pill> getPills()
    {
    	ArrayList<Pill> pl = new ArrayList<Pill>();
    	
    	this.openReadableDB();
        Cursor cursor = db.query(PillTable.TABLE_PILL, null, 
                null, null, null, null, null);        
        while (cursor.moveToNext()) {
             pl.add(getPillFromCursor(cursor));
        }
        if (cursor != null)
            cursor.close();        
        this.closeDB();
    	
    	return pl;
    }
    
    /**
     * Gets contact with specified ID.
     * @param id : ID of Contact object to retrieve
     * @return Contact object corresponding to id, null Contact if not found
     */
    public Pill getPill(long id)
    {	
    	String where = PillTable.COLUMN_ID + "= ?";
        String[] whereArgs = { Long.toString(id) };
        Pill pill;
        
        this.openReadableDB();        
        Cursor cursor = db.query(PillTable.TABLE_PILL, 
                null, where, whereArgs, null, null, null);
        cursor.moveToFirst();
        pill = getPillFromCursor(cursor);
        if (cursor != null)
            cursor.close();
        this.closeDB();
    	
    	return pill;
    }
    
    /**
     * Gets contact data from cursor position in database.
     * @param cursor : Cursor that points to a position in database
     * @return Contact object at cursor position
     */
    private static Pill getPillFromCursor(Cursor cursor)
    {
        if (cursor == null || cursor.getCount() == 0)
        {
            return null;
        }
        else
        {
            try
            {	
            	Pill p = new Pill();
            	p.setPillID(cursor.getInt(0));
            	p.setName(cursor.getString(2));
                
                return p;
            }
            catch(Exception e)
            {
                return null;
            }
        }
    }
    
    /**
     * Inserts a Contact object into the database.
     * @param contact : Contact object to be inserted into database
     * @return rowID of inserted contact, -1 if there was an error
     */
    /*public long insertContact(Contact contact)
    {
    	ContentValues cv = new ContentValues();
    	
        cv.put(CONTACT_ID, contact.getContactID());
        cv.put(CONTACT_NAME, contact.getName());
        cv.put(CONTACT_PHONE, contact.getPhone());
        cv.put(CONTACT_EMAIL, contact.getEmail());
        cv.put(CONTACT_OCCUPATION, contact.getOccupation());
        
        this.openWriteableDB();
        long rowID = db.insert(CONTACT_TABLE, null, cv);
        this.closeDB();
        
        return rowID;
    }*/
    
    /**
     * Updates given Contact object in database 
     * @param contact : Contact object to be updated in database
     * @return Number of rows updated
     */
    /*public int updateContact(Contact contact)
    {
    	ContentValues cv = new ContentValues();
    	
    	cv.put(CONTACT_ID, contact.getContactID());
        cv.put(CONTACT_NAME, contact.getName());
        cv.put(CONTACT_PHONE, contact.getPhone());
        cv.put(CONTACT_EMAIL, contact.getEmail());
        cv.put(CONTACT_OCCUPATION, contact.getOccupation());
        
        String where = CONTACT_ID + "= ?";
        String[] whereArgs = { String.valueOf(contact.getContactID()) };

        this.openWriteableDB();
        int rowCount = db.update(CONTACT_TABLE, cv, where, whereArgs);
        this.closeDB();
        return rowCount;
    }*/
    
    /**
     * Deletes specified contact from database.
     * @param id : The ID of the contact to be deleted from database
     * @return The number of rows deleted
     */
    public int deletePill(long id)
    {
    	String where = PillTable.COLUMN_ID + "= ?";
        String[] whereArgs = { String.valueOf(id) };

        this.openWriteableDB();
        int rowCount = db.delete(PillTable.TABLE_PILL, where, whereArgs);
        this.closeDB();
        
        return rowCount;
    }
    
}
